<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 导入对应产品模块的client
use TencentCloud\Sms\V20190711\SmsClient;
// 导入要请求接口对应的Request类
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
use TencentCloud\Common\Credential;
// 导入可选配置类
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;

// 应用公共文件
function http_request($url, $data = null)
{
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	if (!empty($data)) {
		// curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		curl_setopt($curl, CURLOPT_TIMEOUT, 18);
	} else {
		curl_setopt($curl, CURLOPT_TIMEOUT, 18);
	}

	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($curl);
	$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
	if ($httpCode != 200) {
		var_dump($output);
		echo "<br>http_request失{$httpCode}败：<br>{$url}<br>";
		echo 'Curl error: ' . curl_error($curl);
		echo "<br><br><br><br>";
		curl_close($curl);
		return false;
	} else {
		curl_close($curl);
		return $output;
	}
}

function is_https($url, $way)
{
	if ($way == 1) {
		//验证http
		if (preg_match("/^(http:\/\/|http:\/\/).*$/", $url)) {
			return true;
		} else {
			return false;
		}
	} else {
		//验证https
		if (preg_match("/^(https:\/\/|https:\/\/).*$/", $url)) {
			return true;
		} else {
			return false;
		}
	}
}

/**
 * 服务器数据响应返回，需要返回data的情况
 * @param  [type] $response_code [description]
 * @param  [type] $msg           [description]
 * @param  [type] $data          [description]
 * @return [type]                [description]
 */
function server_response($response_code, $msg, $data = array())
{
	$tmpArr = array();
	$tmpArr['response_code'] = $response_code;
	$tmpArr['msg'] = $msg;
	$tmpArr['data'] = $data;

	return json_encode($tmpArr);
}

//电话验证
function tel_verify($tel)
{
	if (!preg_match("/^1[345789]{1}\d{9}$/", $tel)) {
		return array('code' => 1, 'msg' => '不是手机号码');
	} else {
		return array('code' => 0, 'msg' => '是手机号码');
	}
}

/**
 * 发送短信
 * @param $phone string 手机号
 * @param $content string 短信内容
 * @return bool|string
 */
function sendSms($phone, $content)
{
	// return 0;
	// 短信宝 https://www.smsbao.com/
	// www.smsbao.com/openapi
	// xinyakeji
	// duanxin7xykj
	//测试之前的接口，$phone为数组，implode再访问这个接口，不能成功。故$phone，只应为string
	$smsApi = "http://api.smsbao.com/";
	$user = "xinyakeji"; //短信平台帐号
	$pass = md5("duanxin7xykj"); //短信平台密码
	$sendUrl = $smsApi . "sms?u=" . $user . "&p=" . $pass . "&m=" . $phone . "&c=" . urlencode($content);
	$result = file_get_contents($sendUrl);
	return $result;
}

/*
* 获取一个随机的名字
* @return string $filename，返回一个随机字符串
*/
function getRandomFilename()
{
	//随机文件名规则：年月日时分秒+随机字符串（6）
	$filename = '';

	//设置时区并获取年月日时分秒
	date_default_timezone_set("PRC");
	$filename .= date('YmdHis');

	//获取一个随机字符串
	$str = 'abcdefghijklmnopqrstuvwxyz';
	for ($i = 0; $i < 6; $i++) {
		$filename .= $str[mt_rand(0, 25)];
	}

	//返回文件名
	return $filename;
}

function GetDistance($lat1, $lng1, $lat2, $lng2)
{
	$lat1 = deg2rad($lat1); //纬度
	$lat2 = deg2rad($lat2);
	$lng1 = deg2rad($lng1); //经度
	$lng2 = deg2rad($lng2);

	$a = $lat1 - $lat2; //两纬度之差,纬度<90
	$b = $lng1 - $lng2; //两经度之差纬度<180

	$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($b / 2), 2)));
	$s = $s * 6378.137;
	//返回单位米
	$s = round($s * 10000) / 10000;
	return $s * 1000;
}

function gen($argument)
{
	foreach ($argument as $v) {
		yield $v;
	}
}

//邮箱验证
function email_verify($email)
{
	$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
	if (!preg_match($pattern, $email)) {
		//不是邮箱
		return 1;
	} else {
		//是邮箱
		return 2;
	}
}

// 尝试访问，2秒超时
function http_2s($url)
{
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($curl, CURLOPT_TIMEOUT, 8);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_exec($curl);
	curl_getinfo($curl, CURLINFO_HTTP_CODE);
	curl_close($curl);
};
